local joy__juzhan = fk.CreateSkill {

  name = "joy__juzhan",

  tags = {  },

}



joy__juzhan:addEffect(fk.TargetSpecified, {
  name = "joy__juzhan",
  mute = true,
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(joy__juzhan.name) and data.card.trueName == "slash") then return false end
    if event == fk.TargetConfirmed then
      return player.id ~= data.from and not player.room:getPlayerById(data.from).dead
    else
      return not player.room:getPlayerById(data.to):isNude()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(joy__juzhan.name)
    if event == fk.TargetConfirmed then
        room:notifySkillInvoked(player, joy__juzhan.name, "defensive")
        local from = room:getPlayerById(data.from)
        player:drawCards(1, joy__juzhan.name)
        if from.dead then return end
        from:drawCards(1, joy__juzhan.name)
        if from.dead then return end
        local mark = from:getTableMark(  "@@joy__juzhan-turn")
        table.insertIfNeed(mark, player.id)
        room:setPlayerMark(from, "@@joy__juzhan-turn", mark)
    else
        room:notifySkillInvoked(player, joy__juzhan.name, "control")
        local to = room:getPlayerById(data.to)
        room:moveCardTo(room:askForCardChosen(player, to, "he", joy__juzhan.name), Card.PlayerHand, player, fk.ReasonPrey, joy__juzhan.name, "", false, player.id)
        local mark = player:getTableMark(  "joy__juzhan_red-turn")
        table.insertIfNeed(mark, to.id)
        room:setPlayerMark(player, "joy__juzhan_red-turn", mark)
    end
  end,
})
joy__juzhan:addEffect(fk.TargetConfirmed, {
  name = "joy__juzhan",
  mute = true,
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(joy__juzhan.name) and data.card.trueName == "slash") then return false end
    if event == fk.TargetConfirmed then
      return player.id ~= data.from and not player.room:getPlayerById(data.from).dead
    else
      return not player.room:getPlayerById(data.to):isNude()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(joy__juzhan.name)
    if event == fk.TargetConfirmed then
        room:notifySkillInvoked(player, joy__juzhan.name, "defensive")
        local from = room:getPlayerById(data.from)
        player:drawCards(1, joy__juzhan.name)
        if from.dead then return end
        from:drawCards(1, joy__juzhan.name)
        if from.dead then return end
        local mark = from:getTableMark(  "@@joy__juzhan-turn")
        table.insertIfNeed(mark, player.id)
        room:setPlayerMark(from, "@@joy__juzhan-turn", mark)
    else
        room:notifySkillInvoked(player, joy__juzhan.name, "control")
        local to = room:getPlayerById(data.to)
        room:moveCardTo(room:askForCardChosen(player, to, "he", joy__juzhan.name), Card.PlayerHand, player, fk.ReasonPrey, joy__juzhan.name, "", false, player.id)
        local mark = player:getTableMark(  "joy__juzhan_red-turn")
        table.insertIfNeed(mark, to.id)
        room:setPlayerMark(player, "joy__juzhan_red-turn", mark)
    end
  end,
})

joy__juzhan:addEffect("prohibit", {
  name = "#joy__juzhan_prohibit",
  is_prohibited = function(self, from, to, card)
    return table.contains(from:getTableMark(  "@@joy__juzhan-turn"), to.id)
    or (card and card.color == Card.Red and card.trueName == "slash" and table.contains(from:getTableMark(  "joy__juzhan_red-turn"), to.id))
  end,
})

return joy__juzhan